package com.itggm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itggm.DTO.CoachPageDTO;
import com.itggm.mapper.CoachMapper;
import com.itggm.pojo.Coach;
import com.itggm.response.ResponseResult;
import com.itggm.service.CoachService;
import com.itggm.utils.AliOSSUtil;
import com.itggm.utils.StringUtil;
import com.itggm.vo.CoachVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: ggm
 * @data 2024年11月20日 20:31
 */
@Service
public class CoachServiceImpl implements CoachService {
    @Autowired
    private CoachMapper coachMapper;
    @Autowired
    private AliOSSUtil aliOSSUtil;


    @Override
    public ResponseResult uploadInfo(Coach coach) {
        coachMapper.uploadInfo(coach);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult getCoachInfo(Integer limit) {
        List<Coach> coachList = coachMapper.getCoachInfo(limit);
        return ResponseResult.success().data(coachList);
    }

    @Override
    public void delete(String url) {
        String aliUrl = StringUtil.extractFileNameFromUrl(url);
        aliOSSUtil.delete(aliUrl);
        coachMapper.delete(url);
    }

    @Override
    public ResponseResult page(CoachPageDTO coachPageDTO) {
        PageHelper.startPage(coachPageDTO.getPage(), coachPageDTO.getLimit());
        List<CoachVO> coachVOList = coachMapper.selectByCoachName(coachPageDTO.getCoachName());
        PageInfo<CoachVO> coachVOPageInfo = new PageInfo<CoachVO>(coachVOList);
        return ResponseResult.success().count(coachVOPageInfo.getTotal()).data(coachVOList);

    }

    @Override
    public ResponseResult updateCoachInfo(Coach coach) {
        coachMapper.updateCoachInfo(coach);
        return ResponseResult.success().message("教练信息更新成功");
    }

    @Override
    public ResponseResult getCoachList() {
        List<Coach> coachList = coachMapper.getCoachList();
        return ResponseResult.success().data(coachList);
    }

    /**
     * @Author: ggm
     * @date: 2024/11/26
     * 修改教练图片
     */

    @Override
    public ResponseResult updateImg(String coachId, String fileName, String url) {
        //先删除旧图片
        //阿里云删除图片
        //截取url
        System.out.println("url = " + url);
        String alyUrl = StringUtil.extractFileNameFromUrl(url);
        System.out.println("alyUrl = " + alyUrl);
        aliOSSUtil.delete(alyUrl);
        //修改数据库
        coachMapper.updateCoachImg(coachId, fileName);
        return ResponseResult.success().message("操作成功");
    }
}
